package com.etop.learnapp.activity;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.etop.learnapp.MyItemClickListener;
import com.etop.learnapp.R;
import com.etop.learnapp.adapter.KeyAdapter;
import com.etop.learnapp.util.MyResId;
import com.etop.learnapp.widget.DividerItemDecoration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by John on 2015/10/26 0026.
 */
public class KeyActivity extends AppCompatActivity implements MyItemClickListener {
    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private ArrayList<String> dataset;
    private KeyAdapter mAdapter;

    private int chapterSelect ;
    private int courseSelect;
    private String chapterName;
    private boolean isPractice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key);

        chapterSelect=getIntent().getIntExtra("chapterSelect", -1);
        courseSelect=getIntent().getIntExtra("courseSelect",-1);
        chapterName=getIntent().getStringExtra("chapterName");
        isPractice=getIntent().getBooleanExtra("isPractice",false);
        if(chapterSelect!=-1)
        {
            String title;
            if(!isPractice)
            {
                 title = "key" + chapterSelect  + ".txt";
            }
            else
            {
                title = "pt_key"+chapterSelect + ".txt";
            }

            AssetManager am = getAssets();
            try {
                InputStream is = am.open(title);
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                dataset = new ArrayList<>();
                while ((line = br.readLine()) != null) {
                    dataset.add(line);
                }

        }catch (IOException e) {
                System.out.println("IOException");
            }


            initToolbar();

            recyclerView = (RecyclerView) findViewById(R.id.rv_key);
            recyclerView.setHasFixedSize(true);

            // 创建一个线性布局管理器
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            // 设置布局管理器
            recyclerView.setLayoutManager(layoutManager);
            // 创建Adapter，并指定数据集
            mAdapter = new KeyAdapter(dataset, MyResId.resIDs);
            // 设置Adapter
            recyclerView.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        }
    }

    private void initToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar_include);
        setSupportActionBar(toolbar);
        setTitle(chapterName);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }


    @Override
    public void onItemClick(View view, int position) {
        Intent intent;

        if(isPractice)
        {
            intent = new Intent(KeyActivity.this,PracticeActivity.class);
        }
        else
        {
            intent = new Intent(KeyActivity.this,DetailActivity.class);
        }

            intent.putExtra("chapterSelect",chapterSelect);
            intent.putExtra("courseSelect",courseSelect);
            intent.putExtra("keySelect", position + 1);
            intent.putExtra("keyName",dataset.get(position));
            startActivity(intent);
    }
}
